<!--
  - Copyright (c) 2018.
  - 北京九思金信科技有限公司对该文件内容保留所有权利。
  - 未经许可，不得私自拷贝、传播、发布、引用该文件内容，违者将被追究法律责任。
  -->

<template>
    <div v-if="financeReportAnalysisArray.length > 0">
        <el-row style="margin: 1% 0% 1% 0%">
            <el-col :span="4" align="left">
                <span style="font-size:14px; line-height: 26px; color: #747778">财报分析</span>
            </el-col>
        </el-row>
        <ft-tabs v-model="activeName" @tab-click="tabClick" type="border-card">
            <ft-tab-pane v-for="(tableName, index) in tableArray"
                         :label='tableName'
                         :key="index"
                         :name="tableName">
            </ft-tab-pane>
            <!--财务指标分析-->
            <el-table v-if="this.activeName === '财务指标分析' && handlerFinanceIndicatorArray.length > 0"
                      :data="handlerFinanceIndicatorArray.slice(0, showLines)"
                      :span-method="mergeSameItems"
                      class="el-table"
                      border>
                <el-table-column label="分析名称" prop="itemSetName" align="left"></el-table-column>
                <el-table-column label="指标名称" prop="itemName" align="left"></el-table-column>
                <el-table-column label="指标值" align="center">
                    <el-table-column v-for="(item, index) in reportTerms"
                                     :label="item"
                                     :key="index"
                                     :prop="item"
                                     align="right">
                    </el-table-column>
                </el-table-column>
                <p v-if="showLines !== maxLines" slot="append" style="text-align:center; line-height:15px;">
                    <a href="javascript:" style="color: rgb(0, 98, 222);" @click="showMoreMData()">点击加载更多</a>
                </p>
            </el-table>
            <!--大额科目分析-流动资产-->
            <el-table v-if="this.activeName === '大额科目分析' && handlerSubjectCurrentAssetsArray.length > 0 "
                      :data="handlerSubjectCurrentAssetsArray.slice(0, showLines)">
                <el-table-column label="流动资产" prop="itemName" align="left"></el-table-column>
                <el-table-column v-for="(item, index) in reportTerms"
                                     :label="item"
                                     :key="index"
                                     :prop="item"
                                     align="center">
                    <el-table-column v-for="(name, idx) in ['占流动资产比例', '占总资产比例', '变化比例']"
                                     :label="name"
                                     :key="idx"
                                     :prop="item + name"
                                     align="right">

                    </el-table-column>
                </el-table-column>
                <p v-if="showLines !== maxLines" slot="append" style="text-align:center; line-height:15px;">
                    <a href="javascript:" style="color: rgb(0, 98, 222);" @click="showMoreMData()">点击加载更多</a>
                </p>
            </el-table>
            <!--大额科目分析-非流动资产-->
            <el-table v-if="this.activeName === '大额科目分析' && handlerSubjectNonCurrentAssetsArray.length > 0 && showLines === maxLines"
                      :data="handlerSubjectNonCurrentAssetsArray"
                      class="el-table"
                      border>
                <el-table-column label="非流动资产" prop="itemName" align="left"></el-table-column>
                <el-table-column v-for="(item, index) in reportTerms"
                                 :label="item"
                                 :key="index"
                                 :prop="item"
                                 align="center">
                    <el-table-column v-for="(name, idx) in ['占非流动资产比例', '占总资产比例', '变化比例']"
                                     :label="name"
                                     :key="idx"
                                     :prop="item + name"
                                     align="right">

                    </el-table-column>
                </el-table-column>
            </el-table>
            <!--大额科目分析-流动负债-->
            <el-table v-if="this.activeName === '大额科目分析' && handlerSubjectCurrentDebtsArray.length > 0 && showLines === maxLines"
                      :data="handlerSubjectCurrentDebtsArray"
                      class="el-table"
                      border>
                <el-table-column label="流动负债" prop="itemName" align="left"></el-table-column>
                <el-table-column v-for="(item, index) in reportTerms"
                                 :label="item"
                                 :key="index"
                                 :prop="item"
                                 align="center">
                    <el-table-column v-for="(name, idx) in ['占流动负债比例', '占总负债比例', '变化比例']"
                                     :label="name"
                                     :key="idx"
                                     :prop="item + name"
                                     align="right">

                    </el-table-column>
                </el-table-column>
            </el-table>
            <!--大额科目分析-非流动负债-->
            <el-table v-if="this.activeName === '大额科目分析' && handlerSubjectNonCurrentDebtsArray.length > 0 && showLines === maxLines"
                      :data="handlerSubjectNonCurrentDebtsArray"
                      class="el-table"
                      border>
                <el-table-column label="非流动负债" prop="itemName" align="left"></el-table-column>
                <el-table-column v-for="(item, index) in reportTerms"
                                 :label="item"
                                 :key="index"
                                 :prop="item"
                                 align="center">
                    <el-table-column v-for="(name, idx) in ['占非流动负债比例', '占总负债比例', '变化比例']"
                                     :label="name"
                                     :key="idx"
                                     :prop="item + name"
                                     align="right">
                    </el-table-column>
                </el-table-column>
            </el-table>
        </ft-tabs>
    </div>
</template>

<script>

    export default {
        name: 'FinanceReportAnalysis',
        props: {
            customerId: '',
        },
        data() {
            return {
                activeName: '',
                // 财报分析期次
                reportTerms: [],
                // 财报分析数据
                financeReportAnalysisArray: [],
                // 财务指标数据
                financeIndicatorArray: [],
                // 财务指标数据列表展示
                handlerFinanceIndicatorArray: [],
                // 科目大额分析-流动资产
                subjectCurrentAssetsArray: [],
                // 科目大额分析-流动资产列表展示
                handlerSubjectCurrentAssetsArray: [],
                // 科目大额分析-非流动资产
                subjectNonCurrentAssetsArray: [],
                // 科目大额分析-非流动资产列表展示
                handlerSubjectNonCurrentAssetsArray: [],
                // 科目大额分析-流动负债
                subjectCurrentDebtsArray: [],
                // 科目大额分析-流动负债列表展示
                handlerSubjectCurrentDebtsArray: [],
                // 科目大额分析-非流动负债
                subjectNonCurrentDebtsArray: [],
                // 科目大额分析-非流动负债列表展示
                handlerSubjectNonCurrentDebtsArray: [],
                // 标签名
                tableArray: [],
                // 财务指标分析-分析名称要合并的数组, [2,0,1,3,0,0] 代表第一二行合并,第三行不变,第四五六行合并,
                // 0代表原本的那一行被合并, 即这个列被消除
                setNameSpanArr: [],
                // 要合并数组内容的序号, 即rowIndex
                setNamePosition: 0,
                // 默认只展示8行
                showLines: 8,
                // 最大行数
                maxLines: 999,
            }
        },
        mounted() {
            this.loadData()
        },
        methods: {
            // 展示更多数据
            showMoreMData() {
                // -1 指最后一个元素,但不包含最后一个元素，所以不可使用，暂且使用一个大的整数替代
                this.showLines = this.maxLines
            },
            // 加载数据
            loadData() {
                this.$http.get('/joys-rest/lss-customer/financeReport/analysis/list/' + this.customerId).then(
                    resp => {
                        this.financeReportAnalysisArray = resp.body
                        this.showFatherComponent()
                        this.parseFinanceReportAnalysisData()
                    }, resp => {
                        this.$message.error('财报分析数据加载失败, 请重试!')
                    }
                )
            },
            // 分析财报
            analysisReport() {
                this.$http.put('/joys-rest/lss-customer/financeReport/analysis/' + this.customerId).then(
                    resp => {
                        this.financeReportAnalysisArray = resp.body
                        this.showFatherComponent()
                        this.parseFinanceReportAnalysisData()
                        this.$message.success('分析已完成，请前去查看！')
                    }, resp => {
                        this.$message.error(resp.body)
                    }
                )
            },
            // 解析财报分析数据
            parseFinanceReportAnalysisData() {
                this.clearArrays()
                this.financeReportAnalysisArray.forEach((item, index) => {
                    if (item.analysisType === 'INDICATORS') {
                        if (this.reportTerms.length === 0 && item.analysisInfo[0].itemValueMap) {
                            // 获取分析的财报期次
                            this.reportTerms = Object.keys(item.analysisInfo[0].itemValueMap);
                        }
                        for (let i = 0; i < item.analysisInfo.length; i++) {
                            // 生成新的对象,结构为:{分析名称:xxx, 指标名称:xxx, reportTerm1:xxx, reportTerm2:xxx, reportTerm3:xxx}
                            if (this.handlerFinanceIndicatorArray[i] === undefined) {
                                this.handlerFinanceIndicatorArray[i] = {}
                                this.handlerFinanceIndicatorArray[i].itemSetName = item.analysisInfo[i].itemSetName
                                this.handlerFinanceIndicatorArray[i].itemName = item.analysisInfo[i].itemName
                                for (let j = 0; j < this.reportTerms.length; j++) {
                                    if (item.analysisInfo[i].itemValueMap) {
                                        this.handlerFinanceIndicatorArray[i][this.reportTerms[j]] =
                                            item.analysisInfo[i].itemValueMap[this.reportTerms[j]]
                                    }
                                }
                            }
                        }
                        this.tableArray.push("财务指标分析")
                        // 获取分析名称的合并信息
                        this.getSetNameSpanArr()

                    } else if (item.analysisType === 'SUBJECT') {
                        let subjectAnalysisSetArray = item.analysisInfo
                        for (let i = 0; i < subjectAnalysisSetArray.length; i++) {
                            if (subjectAnalysisSetArray[i]) {
                                if (subjectAnalysisSetArray[i].setName === '流动资产') {
                                    for (let j = 0; j < subjectAnalysisSetArray[i].subjectAnalysisVOS.length; j++) {
                                        if (this.handlerSubjectCurrentAssetsArray[j] === undefined) {
                                            this.handlerSubjectCurrentAssetsArray[j] = {}
                                            this.handlerSubjectCurrentAssetsArray[j].itemName =
                                                subjectAnalysisSetArray[i].subjectAnalysisVOS[j].itemName
                                            for (let k = 0; k < this.reportTerms.length; k++) {
                                                if (subjectAnalysisSetArray[i].subjectAnalysisVOS[j]) {
                                                    this.handlerSubjectCurrentAssetsArray[j][this.reportTerms[k] + '占流动资产比例'] =
                                                        subjectAnalysisSetArray[i].subjectAnalysisVOS[j].ratioMap1[this.reportTerms[k]]
                                                    this.handlerSubjectCurrentAssetsArray[j][this.reportTerms[k] + '占总资产比例'] =
                                                        subjectAnalysisSetArray[i].subjectAnalysisVOS[j].ratioMap2[this.reportTerms[k]]
                                                    this.handlerSubjectCurrentAssetsArray[j][this.reportTerms[k] + '变化比例'] =
                                                        subjectAnalysisSetArray[i].subjectAnalysisVOS[j].changeRatioMap[this.reportTerms[k]]
                                                }
                                            }
                                        }
                                    }
                                }
                                if (subjectAnalysisSetArray[i].setName === '非流动资产') {
                                    for (let j = 0; j < subjectAnalysisSetArray[i].subjectAnalysisVOS.length; j++) {
                                        if (this.handlerSubjectNonCurrentAssetsArray[j] === undefined) {
                                            this.handlerSubjectNonCurrentAssetsArray[j] = {}
                                            this.handlerSubjectNonCurrentAssetsArray[j].itemName =
                                                subjectAnalysisSetArray[i].subjectAnalysisVOS[j].itemName
                                            for (let k = 0; k < this.reportTerms.length; k++) {
                                                if (subjectAnalysisSetArray[i].subjectAnalysisVOS[j]) {
                                                    this.handlerSubjectNonCurrentAssetsArray[j][this.reportTerms[k] + '占非流动资产比例'] =
                                                        subjectAnalysisSetArray[i].subjectAnalysisVOS[j].ratioMap1[this.reportTerms[k]]
                                                    this.handlerSubjectNonCurrentAssetsArray[j][this.reportTerms[k] + '占总资产比例'] =
                                                        subjectAnalysisSetArray[i].subjectAnalysisVOS[j].ratioMap2[this.reportTerms[k]]
                                                    this.handlerSubjectNonCurrentAssetsArray[j][this.reportTerms[k] + '变化比例'] =
                                                        subjectAnalysisSetArray[i].subjectAnalysisVOS[j].changeRatioMap[this.reportTerms[k]]
                                                }
                                            }
                                        }
                                    }

                                }
                                if (subjectAnalysisSetArray[i].setName === '流动负债') {
                                    for (let j = 0; j < subjectAnalysisSetArray[i].subjectAnalysisVOS.length; j++) {
                                        if (this.handlerSubjectCurrentDebtsArray[j] === undefined) {
                                            this.handlerSubjectCurrentDebtsArray[j] = {}
                                            this.handlerSubjectCurrentDebtsArray[j].itemName =
                                                subjectAnalysisSetArray[i].subjectAnalysisVOS[j].itemName
                                            for (let k = 0; k < this.reportTerms.length; k++) {
                                                if (subjectAnalysisSetArray[i].subjectAnalysisVOS[j]) {
                                                    this.handlerSubjectCurrentDebtsArray[j][this.reportTerms[k] + '占流动负债比例'] =
                                                        subjectAnalysisSetArray[i].subjectAnalysisVOS[j].ratioMap1[this.reportTerms[k]]
                                                    this.handlerSubjectCurrentDebtsArray[j][this.reportTerms[k] + '占总负债比例'] =
                                                        subjectAnalysisSetArray[i].subjectAnalysisVOS[j].ratioMap2[this.reportTerms[k]]
                                                    this.handlerSubjectCurrentDebtsArray[j][this.reportTerms[k] + '变化比例'] =
                                                        subjectAnalysisSetArray[i].subjectAnalysisVOS[j].changeRatioMap[this.reportTerms[k]]
                                                }
                                            }
                                        }
                                    }
                                }
                                if (subjectAnalysisSetArray[i].setName === '非流动负债') {
                                    for (let j = 0; j < subjectAnalysisSetArray[i].subjectAnalysisVOS.length; j++) {
                                        if (this.handlerSubjectNonCurrentDebtsArray[j] === undefined) {
                                            this.handlerSubjectNonCurrentDebtsArray[j] = {}
                                            this.handlerSubjectNonCurrentDebtsArray[j].itemName =
                                                subjectAnalysisSetArray[i].subjectAnalysisVOS[j].itemName
                                            for (let k = 0; k < this.reportTerms.length; k++) {
                                                if (subjectAnalysisSetArray[i].subjectAnalysisVOS[j]) {
                                                    this.handlerSubjectNonCurrentDebtsArray[j][this.reportTerms[k] + '占非流动负债比例'] =
                                                        subjectAnalysisSetArray[i].subjectAnalysisVOS[j].ratioMap1[this.reportTerms[k]]
                                                    this.handlerSubjectNonCurrentDebtsArray[j][this.reportTerms[k] + '占总负债比例'] =
                                                        subjectAnalysisSetArray[i].subjectAnalysisVOS[j].ratioMap2[this.reportTerms[k]]
                                                    this.handlerSubjectNonCurrentDebtsArray[j][this.reportTerms[k] + '变化比例'] =
                                                        subjectAnalysisSetArray[i].subjectAnalysisVOS[j].changeRatioMap[this.reportTerms[k]]
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        this.tableArray.push("大额科目分析")
                    }
                })
            },
            // 切换展示的财报的页签
            tabClick(val) {
                this.activeName = val.name
            },
            // 清除数据
            clearArrays() {
                this.reportTerms = []
                this.financeIndicatorArray = []
                this.handlerFinanceIndicatorArray = []
                this.handlerSubjectCurrentAssetsArray = []
                this.handlerSubjectNonCurrentAssetsArray = []
                this.handlerSubjectCurrentDebtsArray = []
                this.handlerSubjectNonCurrentDebtsArray = []
                this.tableArray = []
            },
            // 合并第一列相同单元格
            mergeSameItems({row, column, rowIndex, columnIndex}) {
                //row:行对象，带有一行所有数据，column:列对象
                //rowIndex:行索引，columnIndex:列索引
                //加载表格时从（0，0）开始，（0，1）···（1，0）,(1,1)···依次获取数据
                if (columnIndex === 0) {
                    // 只合并第一列数据
                    let row = this.setNameSpanArr[rowIndex]
                    // 如果被合并了row=0则它这个列需要取消
                    let col = row > 0 ? 1 : 0
                    return {
                        // 相当于给给表格加上rowspan,colspan属性
                        rowspan: row,
                        colspan: col,
                    }
                }
            },
            // 获取分析名称的合并信息
            getSetNameSpanArr() {
                this.setNameSpanArr = [];
                if (this.handlerFinanceIndicatorArray) {
                    for (let i = 0; i < this.handlerFinanceIndicatorArray.length; i++) {
                        if (i === 0) { // 第一行必须是存在的
                            this.setNameSpanArr.push(1);
                            this.setNamePosition = 0
                        } else {
                            // 判断与上一行的这个数据是否相同
                            if (this.handlerFinanceIndicatorArray[i].itemSetName === this.handlerFinanceIndicatorArray[i - 1].itemSetName) {
                                // 相同,上列值 +1
                                this.setNameSpanArr[this.setNamePosition] += 1
                                // 设置本列不展示
                                this.setNameSpanArr.push(0)
                            } else {
                                // 不相同展示本行
                                this.setNameSpanArr.push(1);
                                // 本行的位置
                                this.setNamePosition = i
                            }
                        }
                    }
                }
            },

            // 是否展示父组件
            showFatherComponent() {
                if (this.financeReportAnalysisArray.length === 0) {
                    // 子组件当中修改父组件的visible数据值
                    this.$emit("update:visible",false);
                } else {
                    this.$emit("update:visible",true);
                }
            }
        }


    }

</script>

<style>
    .el-table .cell {
        white-space: pre-wrap;
    }
</style>